package com.xiyueyezi.xiyueyezi.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.xiyueyezi.xiyueyezi.R;

import java.util.Objects;

public class UsbTestActivity extends AppCompatActivity {
    private static final String TAG = "UsbTestActivity";
    private TextView logTextView;

    public static final String ACTION_USB_STATE = "android.hardware.usb.action.USB_STATE";
    public static final String USB_FUNCTION_MTP = "mtp";
    public static final String USB_FUNCTION_PTP = "ptp";
    public static final String USB_FUNCTION_ADB = "adb";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_usb_test);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        logTextView = findViewById(R.id.usb_log);

        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        filter.addAction(ACTION_USB_STATE);
        registerReceiver(usbReceiver, filter);

    }

    public BroadcastReceiver usbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Objects.equals(intent.getAction(), "android.hardware.usb.action.USB_STATE")){
                //获取系统时间
                logTextView.setText(logTextView.getText().toString() + "\n" + System.currentTimeMillis() + "OTG模式切换");
                logTextView.setText(logTextView.getText().toString() + "\n" + "connected: " + intent.getBooleanExtra("connected", false));      //是否连接电脑
                logTextView.setText(logTextView.getText().toString() + "\n" + "mtp: " + intent.getBooleanExtra(USB_FUNCTION_MTP, false));
                logTextView.setText(logTextView.getText().toString() + "\n" + "ptp: " + intent.getBooleanExtra(USB_FUNCTION_PTP, false));
                logTextView.setText(logTextView.getText().toString() + "\n" + "adb: " + intent.getBooleanExtra(USB_FUNCTION_ADB, false));
            }
            if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
                logTextView.setText(logTextView.getText().toString() + "\n" + "插入USB设备");
                UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
                UsbDevice device = usbManager.getDeviceList().values().iterator().next();
                if (usbManager.hasPermission(device)) {
                    logTextView.setText(device.getDeviceName());
                }
            } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
                logTextView.setText(logTextView.getText().toString() + "\n" + "拔出USB设备");
            }
            if (intent.getAction().equals(UsbManager.ACTION_USB_ACCESSORY_ATTACHED)) {
                logTextView.setText(logTextView.getText().toString() + "\n" + "插入usb配件");
            } else if (intent.getAction().equals(UsbManager.ACTION_USB_ACCESSORY_DETACHED)) {
                logTextView.setText(logTextView.getText().toString() + "\n" + "插入usb配件");
            }
        }

    };
}